package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.loginbean.UserBean;
import com.oscar.ehome.bean.pagebean.DaKaInfoBean;
import com.oscar.ehome.bean.pagebean.TongJiMessageBean;
import com.oscar.ehome.bean.pagebean.XueXiListBean;
import com.oscar.ehome.bean.pagebean.XueXiMessageBean;
import com.oscar.ehome.bean.pagebean.tuanduiBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.TuanDuiAdapter;
import com.oscar.ehome.view.pageview.view.TeamCountNewView;
import com.oscar.ehome.view.teamview.TeamCountViewActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/11/22
 */

public class TeamCountNewActivity extends BasesActivity<TeamCountNewView, PagePresenter> implements TeamCountNewView {


    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 展示数据
     */
    @BindView(R.id.appl_gridview)
    GridView applGridview;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.tunaduiqiehuan)
    LinearLayout tunaduiqiehuan;
    @BindView(R.id.text_qiehuan)
    TextView text_qiehuan;
    @BindView(R.id.qiandao_zao)
    TextView qiandao_zao;
    @BindView(R.id.qiandao_xia)
    TextView qiandao_xia;
    @BindView(R.id.qiandao_wan)
    TextView qiandao_wan;
    @BindView(R.id.tuanduitubiao)
    ImageView tuanduitubiao;
    @BindView(R.id.qiandaotubiao)
    ImageView qiandaotubiao;
    @BindView(R.id.xiaoxitubiao)
    ImageView xiaoxitubiao;
    @BindView(R.id.sanxingjinum)
    TextView sanxingjinum;
    @BindView(R.id.xiaoxiname)
    TextView xiaoxiname;
    @BindView(R.id.xiaoxi_weidu)
    TextView xiaoxi_weidu;
    @BindView(R.id.xiaoxi_yidu)
    TextView xiaoxi_yidu;
    @BindView(R.id.qiandaoxiangqing)
    LinearLayout qiandaoxiangqing;
    @BindView(R.id.xiaoxixiangqing)
    LinearLayout xiaoxixiangqing;
    String token = "";
    @BindView(R.id.xuexitubiao)
    ImageView xuexitubiao;
    @BindView(R.id.xuexiname)
    TextView xuexiname;
    @BindView(R.id.xuexi_yidu)
    TextView xuexiYidu;
    @BindView(R.id.xuexi_weidu)
    TextView xuexiWeidu;
    @BindView(R.id.xuexixiangqing)
    LinearLayout xuexixiangqing;
    @BindView(R.id.qiandaoshuaxin)
    ImageButton qiandaoshuaxin;
    @BindView(R.id.xiaoxishuaxin)
    ImageButton xiaoxishuaxin;
    @BindView(R.id.xuexishuaxin)
    ImageButton xuexishuaxin;
    private PagePresenter pagePresenter;
    TuanDuiAdapter adapter;
    int colnum = 3;//adapter 列数
    List<tuanduiBean.ss> tuanduilist = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teamcountnew_activity);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
        //getMessage();
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("团队分析");
        TintManager.setTint(tuanduitubiao, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(text_qiehuan, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(qiandaotubiao, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(xiaoxitubiao, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(xuexitubiao, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(qiandaoshuaxin, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(xiaoxishuaxin, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(xuexishuaxin, Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));


        token = SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getToken();
        text_qiehuan.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getColor()));
        adapter = new TuanDuiAdapter(this, colnum, tuanduilist);
        applGridview.setAdapter(adapter);
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @OnClick({R.id.tunaduiqiehuan, R.id.qiandaoshuaxin,R.id.xiaoxishuaxin,R.id.xuexishuaxin,R.id.title_layout_left, R.id.qiandaoxiangqing, R.id.xiaoxixiangqing,R.id.xuexixiangqing,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.qiandaoshuaxin:
                pagePresenter.dakatongji(token);
                break;
            case R.id.xiaoxishuaxin:
                pagePresenter.xiaoxitongji(token);
                break;
            case R.id.xuexishuaxin:
                pagePresenter.xuexitongji(token);
                break;
            case R.id.xuexixiangqing:
                Intent intent_xuexi = new Intent(TeamCountNewActivity.this, StudyTongJiListActivity.class);
                startActivity(intent_xuexi);
                break;
            case R.id.xiaoxixiangqing:
                Intent intent_xiaoxi = new Intent(TeamCountNewActivity.this, xiaoxiqingActivity.class);
                startActivity(intent_xiaoxi);
                break;
            case R.id.qiandaoxiangqing:
                Intent intent_xiangqing = new Intent(TeamCountNewActivity.this, DakaxiangqingActivity.class);
                startActivity(intent_xiangqing);
                break;
            case R.id.tunaduiqiehuan://编辑
                Intent intent_tj = new Intent(TeamCountNewActivity.this, TeamCountViewActivity.class);
                String user = SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getUserBean();
                UserBean userBean = new Gson().fromJson(user, UserBean.class);
                intent_tj.putExtra("member_name", userBean.getName());
                intent_tj.putExtra("id", SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().getUuid());
                startActivity(intent_tj);
                break;
            case R.id.title_layout_left://返回
                finish();
                break;
        }
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {

            getmessage();
            showLoading();
        } else {
        }
    }

    public void getmessage() {
        pagePresenter.dakatongji(token);
        pagePresenter.rtuanduitongji(token);
        pagePresenter.xiaoxitongji(token);
        pagePresenter.xuexitongji(token);
    }

    @Override
    public void getDaKaMessage(DaKaInfoBean result) {


        if (result != null && !result.equals("")) {

            if (result.getMorning() != null) {
                qiandao_zao.setText(result.getMorning());
            }
            if (result.getAfternoon() != null) {
                qiandao_xia.setText(result.getAfternoon());
            }
            if (result.getNight() != null) {
                qiandao_wan.setText(result.getNight());
            }
        }
    }

    @Override
    public void gettuanduiMessage(tuanduiBean result) {
        Log.e("resss", "gettuanduiMessage: " + result.getThreeStarCount());
        if (result.getThreeStarCount() != 0 && !"".equals(result.getThreeStarCount())) {
            sanxingjinum.setText(result.getThreeStarCount() + "");
        } else {
            sanxingjinum.setText("0");
        }
        if (result.getList() != null && !"".equals(result.getList())) {
            tuanduilist = result.getList();
            adapter.setList(tuanduilist);
        }
    }

    @Override
    public void getxiaoxitongji(TongJiMessageBean result) {
        dismissLoading();
        if (result != null && !result.equals("")) {
            xiaoxi_weidu.setText(result.getUnReadNum());
            xiaoxi_yidu.setText(result.getReadNum());
            xiaoxiname.setText(result.getTitle());
        } else {
            xiaoxi_weidu.setText("0");
            xiaoxi_yidu.setText("0");
            xiaoxiname.setText("最新消息");
        }
    }

    @Override
    public void getxuexitongji(XueXiMessageBean result) {
        dismissLoading();
        if (result != null && !result.equals("")) {
            xuexiWeidu.setText(result.getUnReadNum());
            xuexiYidu.setText(result.getReadNum());
            xuexiname.setText(result.getTitle());
        } else {
            xuexiWeidu.setText("0");
            xuexiYidu.setText("0");
            xuexiname.setText("最新消息");
        }
    }
    @Override
    public void erroy() {
        dismissLoading();
        if (TeamCountNewActivity.this != null)
            ToastUtil.show(TeamCountNewActivity.this, "请求失败，请重试！");
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        if (TeamCountNewActivity.this != null)
            if (result != null) {
                ToastUtil.show(TeamCountNewActivity.this, result + "");
            }
        if (result.contains("过期")) {
            TeamCountNewActivity.this.getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(TeamCountNewActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(TeamCountNewActivity.this, LoginActivity.class);
            startActivity(intent);
            TeamCountNewActivity.this.finish();
        }
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }
}
